package com.qianyanglong.windows.tree;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import com.qianyanglong.windows.vo.ProjectVO;

public class MainTreeModel implements TreeModel{
	
	protected EventListenerList listenerList = new EventListenerList();
	
	protected boolean asksAllowsChildren;
	
	List<ProjectVO> projectList = new ArrayList<ProjectVO>();
	
	DefaultMutableTreeNode root;
	
	List<TreeNode> treeNodes = new ArrayList<TreeNode>();
	
	public MainTreeModel() {
		ProjectVO projectVO1 = new ProjectVO();
		projectVO1.setProjectName("自动点击");
		projectList.add(projectVO1);
		ProjectVO projectVO2 = new ProjectVO();
		projectVO2.setProjectName("JSON格式化");
		projectList.add(projectVO2);
		ProjectVO projectVO3 = new ProjectVO();
		projectVO3.setProjectName("AES加密解密");
		projectList.add(projectVO3);
		root = new DefaultMutableTreeNode("工作空间");
		treeNodes.add(root);
		for (ProjectVO projectVO : projectList) {
			DefaultMutableTreeNode project = new DefaultMutableTreeNode(projectVO.getProjectName());
			root.add(project);;
		}
	}

	@Override
	public Object getRoot() {
		return root;
	}

	@Override
	public Object getChild(Object parent, int index) {
		return ((TreeNode)parent).getChildAt(index);
	}

	@Override
	public int getChildCount(Object parent) {
		return ((TreeNode)parent).getChildCount();
	}

	@Override
	public boolean isLeaf(Object node) {
		if(asksAllowsChildren) {
			 return !((TreeNode)node).getAllowsChildren();
		}
        return ((TreeNode)node).isLeaf();
	}

	@Override
	public void valueForPathChanged(TreePath path, Object newValue) {
		MutableTreeNode aNode = (MutableTreeNode)path.getLastPathComponent();
        aNode.setUserObject(newValue);
	}

	@Override
	public int getIndexOfChild(Object parent, Object child) {
		if(parent == null || child == null) {
			return -1;
		}  
        return ((TreeNode)parent).getIndex((TreeNode)child);
	}

	@Override
	public void addTreeModelListener(TreeModelListener l) {
		listenerList.add(TreeModelListener.class, l);
		
	}

	@Override
	public void removeTreeModelListener(TreeModelListener l) {
		listenerList.add(TreeModelListener.class, l);
		
	}
	
	

}
